'''
Created on 25-11-2012

@author: Maryla
'''
# coding: utf-8
from vboxapi import VirtualBoxManager
from Logger import logger  
 
class VMInfo():
    
    def __init__(self):
        self.mgr = VirtualBoxManager(None, None)
    
    def listMachineNames(self):
        '''
        method return list of virtual machines 
        names + ids
        '''
        vbox = self.mgr.vbox
        machs = self.mgr.getArray(vbox, 'machines')
        names = map(lambda x: (x.name,x.id), machs)
        
        logger.debug("VMs list:")
        logger.debug(names)
        return names
    
    def getAllMachines(self):
        '''
        method return list of virtual machines 
        '''
        vbox = self.mgr.vbox
        machs = self.mgr.getArray(vbox, 'machines')
  
        
        logger.debug("VMs list:")
        logger.debug(machs)
        return machs
    
    def findMachine(self, nameOrId):
        allMachines = self.getAllMachines()
        
        for m in allMachines:
            if m.name == nameOrId or m.id == nameOrId:
                logger.info("Virtual Machine founded")
                return m
      
        logger.warn("Cannot find the machine with name or id " + nameOrId)
        return None
        
    
if __name__ == "__main__":
        vm = VMInfo()
        vm.getAllMachines()
        vm.findMachine("fedora")
      

    